package getSYSU;

import Utils.GHttpPost;
import Utils.GgteHtml;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

//jsoup文档
// https://jsoup.org/cookbook/extracting-data/dom-navigation
public class getPages {
    public String rootHost="https://yz.tongji.edu.cn";
    public String rootPath="/zsxw/bszs.htm";
    public String selectorRootAHerf=".list_main_content ul li a";

    public String basePath="/zsxw/";
    public String selectorPageTitle=".detail_main_content h3";
    public String selectorPageContent="#vsb_content";
    public String tags="同济大学";

    public void setParser(){
        rootHost="http://graduate.sysu.edu.cn";
        rootPath="/gra02/g02b/index.htm";
        selectorRootAHerf="#mainmr ul li a";

        basePath="/gra02/g02b/";
        selectorPageTitle="#cont h1";
        selectorPageContent="#cont";
        tags="";
    }

    public void getRoot() throws Exception {
        setParser();
        //System.out.println(rootHost);
        String html=GgteHtml.get(rootHost,rootPath);
        //System.out.println(html);
        Document doc = Jsoup.parse(html);
        //System.out.println(doc.title());
        Elements newsHeadlines = doc.select(selectorRootAHerf);
        for (Element headline : newsHeadlines) {
            String page=headline.attr("href");
            String path = basePath+page;
            System.out.println(path);
            this.getOnePage(rootHost,path);

        }
    }
    public void getOnePage(String host,String path)  {
        try{
            String html=GgteHtml.get(host,path);
            Document doc = Jsoup.parse(html);
            //System.out.println(doc.title());
            Elements newsHeadlines = doc.select(selectorPageTitle);
//            for (Element headline : newsHeadlines) {
//                System.out.println(headline.text());
//            }
            String title=newsHeadlines.get(0).text();
            Elements newsHeadlines2 = doc.select(selectorPageContent);
            String content=newsHeadlines2.get(0).html();
            //System.out.println(content);
            GHttpPost.post(title+tags,content);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
